/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.scalia.meteocal.gui;

import com.scalia.meteocal.entity.User;
import com.scalia.meteocal.control.UserManager;
import javax.ejb.EJB;
import javax.inject.Named;
import javax.enterprise.context.RequestScoped;

/**
 *
 * @author gabriele
 */
@Named
@RequestScoped
public class RegistrationBean {
    
    @EJB
    private UserManager um;

    private User user;

    public RegistrationBean() {
    }

    public User getUser() {
        if (user == null) {
            user = new User();
        }
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public String register() {
        if (um.checkIfUserExist(user)) {
            return "";
        } else {
            um.save(user);
            return "user/userpage?faces-redirect=true";
        }
    }
    
    public String getCheckUser() {
        if (user == null || user.getUsername() == null) 
            return "";
        return um.checkIfUserExist(user) ? "This username is already taken, change it." : "";
    }
    
}
